package com.face.gaoml.netlibrary.data.cookies;



import com.face.gaoml.netlibrary.data.init.SystemConfig;

import java.io.IOException;

import javax.inject.Inject;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by gml on 16/12/9.
 * 请求 之前 统一 加载的  参数
 *
 *
 */

public class CommonInterceptor implements Interceptor {

    private SystemConfig sc ;//= SystemConfig.getInstance();

    @Inject
    public CommonInterceptor(SystemConfig sc) {
        this.sc =sc;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request oldRequest = chain.request();

        // 添加新的参数
        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host())
                .addQueryParameter("token[mobile_type]", sc.os)
                .addQueryParameter("token[app_version]", sc.version)
                .addQueryParameter("token[device_name]", sc.device_name)
                .addQueryParameter("token[network_type]", sc.network_type)
                .addQueryParameter("token[device_system_version]", sc.device_system_version);


        // 新的请求
        Request newRequest = oldRequest.newBuilder()
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build())
                .build();

        return chain.proceed(newRequest);
    }
}
